package cn.juque.jdfsystem.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.juque.common.base.PageInfo;
import cn.juque.common.constants.MessageEnum;
import cn.juque.common.exception.AppException;
import cn.juque.jdfsystem.entity.RegisterInfo;
import cn.juque.jdfsystem.mapper.RegisterInfoMapper;
import cn.juque.jdfsystem.service.IRegisterInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JUQUE
 * @version 1.0.0
 * <ul>
 *  <li>simple-generator</li>
 *  <li>注册信息</li>
 * </ul>
 * @date 2023-01-10 19:01:26
 **/
@Service("registerInfoService")
public class RegisterInfoServiceImpl implements IRegisterInfoService {

    @Resource
    private RegisterInfoMapper registerInfoMapper;

    /**
     * 分页查询
     *
     * @param pageInfo     pageInfo
     * @param registerInfo registerInfo
     * @return DataGrid
     */
    @Override
    public List<RegisterInfo> listInfoWithPage(PageInfo pageInfo, RegisterInfo registerInfo) {
        Page<RegisterInfo> page = new Page<>(pageInfo.getPage(), pageInfo.getLimit());
        List<RegisterInfo> list = this.registerInfoMapper.queryInfoList(page, registerInfo);
        pageInfo.setTotal(page.getTotal());
        return list;
    }

    /**
     * 查询
     *
     * @param registerInfo registerInfo
     * @return List
     */
    @Override
    public List<RegisterInfo> listInfoWithoutPage(RegisterInfo registerInfo) {
        return this.registerInfoMapper.queryInfoList(registerInfo);
    }

    /**
     * 保存
     *
     * @param registerInfo registerInfo
     */
    @Override
    public void saveInfo(RegisterInfo registerInfo) {
        boolean flag = SqlHelper.retBool(this.registerInfoMapper.insert(registerInfo));
        if (!flag) {
            throw new AppException(MessageEnum.SAVE_FAIL);
        }
    }

    /**
     * 删除
     *
     * @param ids 主键列表
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteInfo(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        int count = this.registerInfoMapper.deleteBatchIds(ids);
        if (count != ids.size()) {
            throw new AppException(MessageEnum.DELETE_FAIL);
        }
    }

}
